import util from '@/libs/util';
import Cookies from 'js-cookie';
import memberList from '@/views/components/member/member.vue';

export default {
    name: 'exportable-table',
    components: { memberList },
    data () {
        return {
            columns: [{
                type: 'selection',
                width: 60,
                align: 'center'
            }, {
                title: '标题',
                key: 'title'
            }, {
                title: '创建时间',
                key: 'createTime'
            }, {
                title: '操作',
                key: 'action',
                width: 300,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.detail(params.row);
                                }
                            }
                        }, '详情'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.infoPush(params.row.id);
                                }
                            }
                        }, '推送')
                    ]);
                }
            }],
            data: [],
            formData: {},
            viewModal: false,
            editModal: false,
            pushModal: false,

            total: 0,
            pageIndex: 1,
            pageSize: 10,
            baseUser: JSON.parse(Cookies.get('baseUser')),
            id: '',
            ids: [],
            memberIds: [],
            pushId: '',
            ruleInline: {
                title: [
                    { required: true, message: '标题不能为空！', trigger: 'blur' }
                ],
                content: [
                    { required: true, message: '内容不能为空！', trigger: 'blur' }
                ]
            }
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/info/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                orgId: this.baseUser.orgId
            })).then(res => {
                if (res.data.data) {
                    var list = res.data.data.list;
                    for (var i = 0; i < list.length; i++) {
                        list[i]['createTime'] = util.getDate(list[i].createTime);
                    }
                    this.data = res.data.data.list;
                    this.total = res.data.data.total;
                }
            });
        },
        detail (obj) {
            util.ajax.post('/busine/info/detail', util.toFormData({
                id: obj.id
            })).then(res => {
                if (res.data.data) {
                    var data = res.data.data;
                    data['createTime'] = util.getDate(data.createTime);
                    this.formData = res.data.data;
                    this.viewModal = true;
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        showAdd () {
            this.formData = {};
            this.editModal = true;
        },
        showEdit (obj) {
            this.formData = {};
            util.ajax.post('/busine/info/detail', util.toFormData({
                id: obj.id
            })).then(res => {
                if (res.data.data) {
                    var li = res.data.data;
                    li['createTime'] = util.getDate(li.createTime);

                    this.formData = res.data.data;
                    this.editModal = true;
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        save (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let formData = {};
                    for (let key in this.formData) {
                        formData[key] = this.formData[key];
                    }
                    formData['orgId'] = this.baseUser.orgId;
                    formData['createBy'] = this.baseUser.id;
                    util.ajax.post('/busine/info/save', util.toFormData(formData)).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.editModal = false;
                            this.$Message.success('保存成功！');
                            this.pageIndex = 1;
                            this.loadList();
                        } else {
                            this.$Message.warning(res.data.message || '未知错误！');
                        }
                    });
                }
            });
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/busine/info/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        getSelect (ref) {
            this.ids = [];
            ref.forEach((item) => {
                this.ids.push(item.id);
            });
        },
        deleteByIds () {
            if (this.ids.length > 0) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '<p>确定删除所选信息？</p>',
                    onOk: () => {
                        util.ajax.post('/busine/info/deleteByIds', util.toFormData({
                            ids: this.ids
                        })).then(res => {
                            if (res.data.message === 'SUCCESS') {
                                this.$Message.success('删除成功！');
                                this.loadList();
                            }
                        });
                    }
                });
            } else {
                this.$Modal.error({
                    content: '请选择要删除信息！'
                });
            }
        },
        infoPush (id) {
            this.$refs.memberList.loadList();
            this.pushModal = true;
            this.pushId = id;
        },
        memberSelect (memberIds) {
            this.memberIds = memberIds;
        },
        pushSave () {
            if (!this.memberIds || this.memberIds.length <= 0) {
                this.$Modal.error({
                    content: '请选择学员！'
                })
                return false;
            }
            util.ajax.post('/busine/info/member/save', util.toFormData({
                pushId: this.pushId,
                memberIds: this.memberIds
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('推送成功！');
                    this.loadList();
                }
            });
            this.pushModal = false;
        }
    }
};